package com.newasia.management.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.newasia.management.ManagementOrderService;
import com.newasia.management.constants.ManagementRetCode;
import com.newasia.management.converter.OrderConverter;
import com.newasia.management.dal.order.entitys.Order;
import com.newasia.management.dal.order.entitys.OrderItem;
import com.newasia.management.dal.order.entitys.OrderItemExample;
import com.newasia.management.dal.order.entitys.OrderShipping;
import com.newasia.management.dal.order.persistence.OrderItemMapper;
import com.newasia.management.dal.order.persistence.OrderMapper;
import com.newasia.management.dal.order.persistence.OrderShippingMapper;
import com.newasia.management.dto.order.*;
import com.newasia.management.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ManagementOrderServiceImpl implements ManagementOrderService{

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter orderConverter;

    /**
     * 查询历史订单列表
     * @param request
     * @return
     * @author GP17513-成都-Rigel
     */
    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        OrderListResponse response = new OrderListResponse();
        try{
            request.requestCheck();
            response.setCode(ManagementRetCode.SUCCESS.getCode());
            response.setMsg(ManagementRetCode.SUCCESS.getMessage());
            PageHelper.startPage(request.getPage(),request.getSize());

            List<Order> orderList = orderMapper.selectOrderByUserId(request.getKey(),
                    request.getUserId(), request.getStatus().size(), request.getStatus(), request.getSort());
            if(CollectionUtils.isEmpty(orderList)){
                response.setTotal(0L);
                response.setDetailInfoList(new ArrayList<>());
                return response;
            }
            List<OrderDetailInfo> infos = new ArrayList<>();
            PageInfo<Order> pageInfo=new PageInfo<>(orderList);
            response.setTotal(pageInfo.getTotal());
            orderList.forEach( order -> {
                OrderDetailInfo info = orderConverter.order2detail(order);
                OrderItemExample itemExample=new OrderItemExample();
                itemExample.createCriteria().andOrderIdEqualTo(order.getOrderId());
                List<OrderItem> list=orderItemMapper.selectByExample(itemExample);
                OrderShipping orderShipping=orderShippingMapper.selectByPrimaryKey(order.getOrderId());
                info.setOrderItemDto(orderConverter.item2dto(list));
                info.setOrderShippingDto(orderConverter.shipping2dto(orderShipping));
                infos.add(info);
            });
            response.setDetailInfoList(infos);
        }catch (Exception e){
            log.info("OrderQueryServiceImpl.orderList occur Exception: {}" , e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }

    /**
     * 查询订单明细
     * @param request
     * @return
     */
    @Override
    public OrderDetailResponse orderDetail(OrderDetailRequest request) {
        OrderDetailResponse response=new OrderDetailResponse();
        try{
            request.requestCheck();
            Order order=orderMapper.selectByPrimaryKey(request.getOrderId());
            OrderItemExample example=new OrderItemExample();
            OrderItemExample.Criteria criteria=example.createCriteria();
            criteria.andOrderIdEqualTo(order.getOrderId());
            List<OrderItem> list=orderItemMapper.selectByExample(example);
            OrderShipping orderShipping=orderShippingMapper.selectByPrimaryKey(order.getOrderId());
            response=orderConverter.order2res(order);
            response.setOrderItemDto(orderConverter.item2dto(list));
            response.setOrderShippingDto(orderConverter.shipping2dto(orderShipping));
            response.setCode(ManagementRetCode.SUCCESS.getCode());
            response.setMsg(ManagementRetCode.SUCCESS.getMessage());
            return response;
        }catch (Exception e){
            log.error("OrderQueryServiceImpl.orderDetail occur Exception :" +e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }

    @Override
    public UpdateOrderResponse updateOrder(UpdateOrderRequest request) {
        log.error("OrderQueryServiceImpl.updateOrder :" +request);
        UpdateOrderResponse response = new UpdateOrderResponse();
        response.setCode(ManagementRetCode.SUCCESS.getCode());
        response.setMsg(ManagementRetCode.SUCCESS.getMessage());
        try{
            request.requestCheck();
            Order order = new Order();
            order.setOrderId(request.getOrderId());
            order.setPaymentType(request.getPaymentType());
            order.setPostFee(request.getPostFee());
            order.setStatus(request.getStatus());
            order.setPaymentTime(request.getPaymentTime());
            order.setConsignTime(request.getConsignTime());
            order.setEndTime(request.getEndTime());
            order.setCloseTime(request.getCloseTime());
            order.setShippingName(request.getShippingName());
            order.setShippingCode(request.getShippingCode());
            order.setBuyerNick(request.getBuyerNick());
            order.setBuyerMessage(request.getBuyerMessage());
            orderMapper.updateByPrimaryKeySelective(order);
        }catch(Exception e){
            log.error("OrderQueryServiceImpl.updateOrder occur Exception :" +e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }
}
